//
//  SevenPresentTransitionViewController.swift
//  swiftTS
//
//  Created by 航电 on 2020/4/15.
//  Copyright © 2020 航电. All rights reserved.
//

import UIKit

class SevenPresentTransitionViewController: UIViewController {
    
    fileprivate let cellID = "Seven_PresentTransition";
    fileprivate var mutDataArr:NSMutableArray = {
        var mutArr = NSMutableArray();
        mutArr.add(["img":"picture_01","title":"第一张"]);
        mutArr.add(["img":"picture_01","title":"第二张"]);
        mutArr.add(["img":"picture_01","title":"第三张"]);
        mutArr.add(["img":"picture_01","title":"第四张"]);
        mutArr.add(["img":"picture_01","title":"第五张"]);
        return mutArr;
    }();
    public var _selectCell:EleventCell?;
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "present";
        self.view.backgroundColor = .lightGray;
        // Do any additional setup after loading the view.
        self.onCreateUIForView();
    }
    

    //MARK:UI
    fileprivate func onCreateUIForView() {
        let lb = UILabel(frame: CGRect(x: Tool.kScreenWith(width: 10), y: Tool.HEIGHT_STATUS_NAVBAR+Tool.kScreenWith(width: 10), width: screenMainSize.width-Tool.kScreenWith(width: 10)*2, height: screenMainSize.height-Tool.HEIGHT_STATUS_NAVBAR-Tool.kScreenWith(width: 10)));
        lb.backgroundColor = .clear
        lb.text = "我们在做一些app的时候经常会用到详情页，评价页，总之就是点一个按钮就展示一些信息，在做一些简单的展示或者小逻辑。一般都会presentViewController. \n 大家可以下载我的代码，看看一些跟过渡动画没有关系的设置，比如文字，和半透明背景 ，下面UIScrollView等等 ， 因为他们不是本节要介绍的重点，本节要介绍的重点是自定义过渡动画。\n首先，创建一个Single View Application，然后在Main.storyboard中定义好搞两个界面，定义好约束 。 不懂的可以下载我源码。看源码上，也可以不搞这么复杂，随便搞两个页面 练习过渡动画就行。";
        lb.numberOfLines = 0;
        lb.textAlignment = .left;
        self.view.addSubview(lb);
        
        let flowLayout = UICollectionViewFlowLayout();
        flowLayout.scrollDirection = .horizontal;
        let collectionView = UICollectionView(frame: CGRect(x: 0, y: screenMainSize.height-100, width: screenMainSize.width, height: 100), collectionViewLayout: flowLayout);
        collectionView.delegate = self;
        collectionView.dataSource = self;
        collectionView.backgroundColor = .clear;
        collectionView.register(EleventCell.self, forCellWithReuseIdentifier: cellID);
        self.view.addSubview(collectionView);
    }
}

extension SevenPresentTransitionViewController:UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.frame.height, height: collectionView.frame.height);
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0);
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return Tool.kScreenWith(width: 20);
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0;
    }
}

extension SevenPresentTransitionViewController:UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if self.mutDataArr.count > indexPath.row {
            _selectCell = (collectionView.cellForItem(at: indexPath) as! EleventCell);
            
            let vc = SevenPresentTransitionDetailViewController();
            vc.modalPresentationStyle = .fullScreen;
            vc.transitioningDelegate = self;
            self.present(vc, animated: true, completion: nil);
            vc.dataDic = (self.mutDataArr[indexPath.row] as! [String:AnyObject]);
        }
    }
}

extension SevenPresentTransitionViewController:UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.mutDataArr.count;
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:EleventCell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! EleventCell;
        if self.mutDataArr.count > indexPath.row {
            cell.dataDic = (self.mutDataArr[indexPath.row] as! [String:AnyObject]);
        }
        return cell
    }
}

extension SevenPresentTransitionViewController:UIViewControllerTransitioningDelegate {
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return SevenPresentTransition.transitionWithTransitionType(type: .SevenPresentTransitionType_Present);
    }

    //返回nil，说明使用默认动画
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return nil;
    }
}
